package gui.Server;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.SystemColor;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.border.TitledBorder;

import system.InitiateServer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/**
 * this class use as the main menu for server
 * @author G8
 *
 */
public class ServerGUI extends JPanel implements ActionListener{
	private JPasswordField passwordField;
	public JButton btnConnect;
	private JLabel lblDateTime;
	private JPasswordField passwordFieldDB;
	private JTextField textFieldPort;
	private ServerRunningScreen serverRunningScreen;
	public static InitiateServer sv;
	/**
	 * Server GUI Screen Constructor 
	 */
	public ServerGUI(){
		super();
		initialize();
	}
		
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		setBorder(new TitledBorder(null, "VCP Server", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.menu);
		setBounds(InitiateServer.guiX,InitiateServer.guiY,500,400);
		setLayout(null);
		
		JLabel lblWelcomeToVcp = new JLabel("Welcome to VCP System by Group 8 !");
		lblWelcomeToVcp.setBounds(52, 29, 366, 14);
		add(lblWelcomeToVcp);
		
		JLabel lblPleaseInsertThe = new JLabel("Please insert the database password in order to enter the system...");
		lblPleaseInsertThe.setBounds(52, 54, 366, 14);
		add(lblPleaseInsertThe);
		
		lblDateTime = new JLabel("Date and Time");
		lblDateTime.setBounds(201, 332, 97, 14);
		lblDateTime.setText(InitiateServer.getDateTime());
		add(lblDateTime);
		
		JLabel lblDatabasePassword = new JLabel("Database Password : ");
		lblDatabasePassword.setBounds(102, 113, 135, 14);
		add(lblDatabasePassword);
		
		passwordFieldDB = new JPasswordField();
		passwordFieldDB.setBounds(247, 110, 135, 20);
		add(passwordFieldDB);
		
		btnConnect = new JButton("Connect");
		btnConnect.setBounds(175, 180, 123, 45);
		btnConnect.addActionListener(this);
		add(btnConnect);
		
		JLabel labelPort = new JLabel("Port :");
		labelPort.setBounds(102, 141, 135, 14);
		add(labelPort);
		
		textFieldPort = new JTextField();
		textFieldPort.setText("5555");
		textFieldPort.setBounds(247, 138, 135, 20);
		add(textFieldPort);
		textFieldPort.setColumns(10);
		
		
	}

	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==btnConnect){
			try
		    {
		      InitiateServer.port = Integer.parseInt(textFieldPort.getText()); //Get port from command line
		    }
		    catch(Throwable t)
		    {
		    	InitiateServer.port = InitiateServer.DEFAULT_PORT; //Set port to 5555
		    }
			String pass = passwordFieldDB.getText();
			if (!pass.equals("Braude"))
				JOptionPane.showMessageDialog(null, "Incorrect Password !", "Error",
                        JOptionPane.ERROR_MESSAGE);
			else
			{
				sv = new InitiateServer(InitiateServer.port, passwordFieldDB.getText());
		        try 
			    {
			      sv.listen(); //Start listening for connections
			      JOptionPane.showMessageDialog(null, "Server is running !", "Success",
	                      JOptionPane.INFORMATION_MESSAGE);
			      serverRunningScreen = new ServerRunningScreen();
			      InitiateServer.setScreen(this, serverRunningScreen);
			    } 
			    catch (Exception ex) 
			    {
			    	JOptionPane.showMessageDialog(null, "Unable to create a connection !", "Error",
	                        JOptionPane.ERROR_MESSAGE);
			    }
			}
	  }		
	}
}